/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.jomijushi.fid.servicios;

import com.jomijushi.fid.dao.DAO;
import com.jomijushi.fid.dao.JpaDao;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;

/**
 *
 * @author jcgrados
 */
public class JpaService<K, E> implements Service<K, E> {

    protected Class<E> entityClass;
    protected DAO<K, E> dao;
    @PersistenceContext
    protected EntityManagerFactory emf;

    public JpaService(EntityManagerFactory emf) {
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
		this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
		this.dao = new JpaDao<K, E>(1);
        this.emf = emf;
	}

      public JpaService() {          
        ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();       
		this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];                
        this.dao = new JpaDao<K, E>(1);
	 }


      public void setEntityManagerFactory(EntityManagerFactory emf){
          this.emf = emf;
      }


      public void registrar(E entity) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try{
            dao.setEntityManager(em);
            dao.persist(entity);
            em.getTransaction().commit();
        }catch(Exception e)
        {
            em.getTransaction().rollback();
        }finally{
            em.close();
        }
    }


    public void modificar(E entity) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try{
            dao.setEntityManager(em);
            dao.merge(entity);
            em.getTransaction().commit();
        }catch(Exception e)
        {
            em.getTransaction().rollback();
        }finally{
            em.close();
        }
    }


    public E buscar(K id) {
        EntityManager em = emf.createEntityManager();       
        em.getTransaction().begin();
        try{
            dao.setEntityManager(em);
         
            E class1 = (E) dao.findById(this.entityClass,id);           
            em.getTransaction().commit();       
            return     class1;
        }
        catch(Exception e){
            return null;
        }
        finally {
            em.close();
        }
    }


    public void eliminar(E entity) {
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try{
            dao.setEntityManager(em);
            dao.remove(entity);
            em.getTransaction().commit();
        }
        catch(Exception e)
        {
            em.getTransaction().rollback();
        }finally{
            em.close();
        }
    }

    public List<E> reportar(String keyword, Object valorKey, String ordenarBy, String criterioOrden, boolean verTodo, boolean isLike, int limInf, int limSup) {
        EntityManager em = emf.createEntityManager();        
        List<E> lstE = new ArrayList<E>();
        try{
            dao.setEntityManager(em);
            dao.setEntityClass(entityClass);           
            lstE = (List<E>)dao.reportar(keyword, valorKey, ordenarBy, criterioOrden, verTodo, isLike, limInf, limSup);       
        }
        catch(Exception e)
        {           
           System.out.println("Service Error: No se realiza el reporte de manera correcta\n"+e.getCause().toString()+"\n"+e.getMessage());
        }
      
        return lstE;      
    }


     public List<E> reportar(String nombreHijo, String keyword, Object valorKey, String ordenarBy, String criterioOrden, boolean verTodo, boolean isLike, int limInf, int limSup) {        
        EntityManager em = emf.createEntityManager();         
        List<E> lstE = new ArrayList<E>();
        try{
            dao.setEntityManager(em);
            dao.setEntityClass(entityClass);          
            lstE = (List<E>)dao.reportar(nombreHijo, keyword, valorKey, ordenarBy, criterioOrden, verTodo, isLike, limInf, limSup);          
        }
        catch(Exception e)
        {      
           System.out.println("Service Error: No se realiza el reporte de manera correcta\n"+e.getCause().toString()+"\n"+e.getMessage());
        }
        return lstE;
    }



    public E buscar(String keyword, Object valorKey) {
        EntityManager em = emf.createEntityManager();        
        E key = null;
        try{            
            dao.setEntityManager(em);
            dao.setEntityClass(entityClass);
            key = (E)dao.buscar(keyword, valorKey);
        }
        catch(Exception e){
           System.out.println("Service Error: No se realiza la busqueda de manera correcta\n"+e.getCause().toString()+"\n"+e.getMessage());
        }
       
        return key;
    }

    public E buscar(String nombreHijo, String keyword, Object valorKey) {
        EntityManager em = emf.createEntityManager();     
        E key = null;
        try{
            dao.setEntityManager(em);
            dao.setEntityClass(entityClass);
            key = (E)dao.buscar(nombreHijo, keyword, valorKey);
        }
        catch(Exception e){
              System.out.println("Service Error: No se realiza la busqueda de manera correcta\n"+e.getCause().toString()+"\n"+e.getMessage());
              return key;
        }       
        return key;
    }




}
